<?php 
/**
	Script used to add a new user to the plateform and add his data in the table `account`
*/
require_once('Class/session.Class.php');
require_once('Class/connexionDB.Class.php');
require_once('Class/account.Class.php');

$session = session::getInstance();

if (isset($session->id_accountType) && $session->id_accountType == '1')
{
	$db 		= new connexionDB();
	$account 	= new account();
	$errorTab 	= array();
	$error 		= '';
	
	if (!$account->isValidEmail($_POST['emailNewAccount']))
	{
		$errorTab['email'] = true;
	}

	if (!$account->isValidPassword($_POST['passwordNewAccount'], $_POST['passwordRepeat']))
	{
		$errorTab['password'] = true;
	}

	if (!isset($_POST['accountType']) || !$account->isValidAccountType($_POST['accountType']))
	{
		$errorTab['accountType'] = true;
	}

	$groups = implode(';', $account->listedGroup($_POST)) . ';';

	if (empty($errorTab))
	{
		$newAccount 					= array();
		$newAccount['email'] 			= $_POST['emailNewAccount'];
		$newAccount['password'] 		= md5($_POST['passwordNewAccount']);
		$newAccount['firstname'] 		= $_POST['firstnameNewAccount'];
		$newAccount['lastname'] 		= $_POST['lastnameNewAccount'];
		$newAccount['id_account_type'] 	= $_POST['accountType'];
		$newAccount['groups'] 			= $groups;
		if (isset($_POST['countryNewAccount']))
		{
			$newAccount['country'] 		= $_POST['countryNewAccount'];	
		}
		else
		{
			$newAccount['country'] 		= '';	
		}
		
		$newAccount['team']				= $_POST['team'];
		
		$account->add($newAccount);
		header("Location: administration.php?accountcreated=". $newAccount['email']);
		exit;
	}
	else 
	{
		foreach ($errorTab as $key => $value) {
			$error .= "error". $key . "=" . $value . "&";
		}
		header("Location: administration.php?$error");
		exit;
	}
}
else
{
	header("Location: administration.php?error=notallowedtocreateaccount");
	exit;
}
?>